<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="600px"
    top="5%"
  >
    <t-form ref="form" :colon="true"  :data="formData" labelWidth="120px" :rules="rules">
      <t-form-item label="子系统名称" name="serviceName">
        <t-input clearable v-model="formData.serviceName" :maxLength="100"  placeholder="请输入子系统名称"></t-input>
      </t-form-item>
      <t-form-item label="子系统code" name="serviceCode">
        <t-input clearable v-model="formData.serviceCode" :maxLength="100" placeholder="请输入子系统code"></t-input>
      </t-form-item>
<!--      <t-form-item label="子系统路由" name="serviceRout">-->
<!--        <t-input clearable v-model="formData.serviceRout" :maxLength="100" placeholder="请输入子系统路由"></t-input>-->
<!--      </t-form-item>-->
<!--      <t-form-item label="图标" name="sysAttMainIds">-->
<!--            <image-upload v-model="formData.sysAttMainIds" :max="1"></image-upload>-->
<!--      </t-form-item>-->
      <t-form-item label="是否可申报成果" name="isBusinessService">
        <t-switch
          size="large"
          v-model="formData.isBusinessService"
          :label="['是', '否']"
          :customValue="['1', '0']"
        ></t-switch>
      </t-form-item>
      <t-form-item label="排序号" name="serviceSort">
        <t-input-number clearable :fileLists="fileLists" v-model="formData.serviceSort" :min="1" placeholder="请输入"></t-input-number>
      </t-form-item>
      <t-form-item label="备注" name="remark">
        <t-textarea
          v-model="formData.remark"
          placeholder="请输入内容"
        />
      </t-form-item>
    </t-form>
  </t-dialog>
</template>

<script>

export default {
  name: "add-edit-subsystem",
  components:{
  },
  props:["header","serviceId"],
  data() {
    return {
      visible: true,
      loading: false,
      fileLists: [],
      formData: {
        serviceName: "",
        serviceCode: "",
        serviceRout: "",
        sysAttMainIds: "",
        remark: "",
        serviceSort: 1,
        isBusinessService:'0'
      },
      rules: {
        serviceName: [{ required: true, message: "微服务名称不能为空" }],
        serviceCode: [{ required: true, message: "微服务code不能为空" }],
        serviceRout: [{ required: true, message: "服务路由不能为空" }],
        sysAttMainIds: [{ required: true, message: "服务图标不能为空" }],
        serviceSort: [
          { required: true, message: "排序号不能为空" },
          {
            pattern: /(^[1-9]\d*$)/,
            message: '请输入正整数'
          }
        ]
      },
    }
  },
  watch: {
    serviceId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    }
  },
  methods:{
    getObj(){
      this.$api.system.subsystem.sysServiceDetail(this.serviceId).then((response) => {
        response.data.serviceSort =  Number(response.data.serviceSort);
        this.formData = response.data;
        // getFiles(this.serviceId).then((res) => {
        //   this.fileLists = []
        //   const ids = [];
        //   if (res.data && res.data.length) {
        //     res.data.forEach((item) => {
        //       ids.push(item.id);
        //       this.fileLists.push({ name: item.fileName, url: item.fullFilePath, id: item.id, filePath: item.filePath })
        //     })
        //     this.formData.sysAttMainIds = ids.join(',');
        //   }
        // });
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.serviceId !== 0) {
            this.$api.system.subsystem.sysServiceUpdate(this.formData).then((response) => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            this.$api.system.subsystem.sysServiceAdd(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
